<?php
	
require '../include/user_login.inc.php';
require '../include/paging.inc.php';

	
	$task_id = $sef_url;
	$user_id = $_SESSION['USER_SID'];
		 
	if($_SESSION['ACCESS_SID'] == 1) $sql = "SELECT * FROM  tasks
	WHERE task_id = \"".$task_id."\" AND (by_user = '".$_SESSION['USER_SID']."' OR to_user = '".$_SESSION['USER_SID']."')";
	
	if($_SESSION['ACCESS_SID'] == 2)  $sql = "SELECT * FROM  
	tasks AS t,
	law_users AS l
	WHERE 
	t.task_id = '".$task_id."' 
	AND 
	l.law_id = '$user_id'
	AND
	l.use_id = t.by_user
	";
	
	$result = query($sql);
	$row_note = mysql_fetch_object($result);
	
	if (!$row_note) redirect(HTML_BASE.$_SESSION["lang"].'/login/');
	
	if (!isset($_POST['is_submitted'])) {
		$_POST['is_submitted'] = '';
	}
	
	if ($_POST['is_submitted']) {
		$error_message = '';
		$redirect = $_POST["redirect"];
		if (!$_POST['description']) $error_message .= 'Не е въведено съдържание <br/>';
		
		if ($_FILES['file1']['name'] || $_FILES['file2']['name'] || $_FILES['file3']['name'] )
		{
			 if(!is_dir("../files/task-".$task_id)) mkdir("../files/task-".$task_id, 0755); 
			$folder = $user_id.date("-j-M-G-i-s");
			
		}
		
		
		
		
		if(!$error_message) {
				
			//Ъпдейт в ДБ
	
				$sql = "INSERT INTO tasks_history
				  (task_id,by_user,description,files)
				VALUES
				  ('".$task_id."','".$user_id ."','".escape($_POST['description'])."','$folder')";
		
			query($sql);
			
			
			$sql = "UPDATE tasks
						 SET
						 priority = '".$_POST['priority']."'
						 WHERE
						 task_id = '".$task_id."'
					   
					";

				query($sql);
			
			if($folder)
			{
				$path = "../files/task-".$task_id."/".$folder."/";
				if(!is_dir($path)) mkdir($path, 0755);
				
				if ($_FILES['file1']['name'])
				{
					$target_path = $path.basename( $_FILES['file1']['name']); 

					if(!move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) {
					
						 $error_message .= 'Upload Error!';
					}
				}
				
				if ($_FILES['file2']['name'])
				{
					$target_path = $path.basename( $_FILES['file2']['name']); 

					if(!move_uploaded_file($_FILES['file2']['tmp_name'], $target_path)) {
					
						 $error_message .= 'Upload Error!';
					}
				}
				
				if ($_FILES['file3']['name'])
				{
					$target_path = $path.basename( $_FILES['file3']['name']); 

					if(!move_uploaded_file($_FILES['file3']['tmp_name'], $target_path)) {
					
						 $error_message .= 'Upload Error!';
					}
				}
				
			}
			
			
			
		redirect(HTML_BASE.$_SESSION["lang"].'/correspondence/'.$task_id);
		}


	}else{
		$redirect = $_SERVER['HTTP_REFERER'];	
	}
	
	
	
	head($row_note->title);
	if($_SESSION['ACCESS_SID'] == 1) menu_user();
	if($_SESSION['ACCESS_SID'] == 2) menu_law();
	
	

?>

<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	language : "<?=$_SESSION["lang"]?>", 
	mode : "exact",
    elements : "body1",
	theme : "advanced",
	relative_urls : false,
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,

	// Drop lists for link/image/media/template dialogs
	//template_external_list_url : "js/template_list.js",
	//external_link_list_url : "js/link_list.php",
	//media_external_list_url : "js/media_list.js",
	//external_image_list_url : "js/blog_image_list.php"
	
});
</script>
	<div id="content">
    
            <?php if ($error_message) { ?>
			<div class="message red">
				<?=$error_message?>
			</div>
			<?php } ?>
			
			
            <?php if($row_note->priority == 1) echo "<div id=\"urgent\">".__URGENT."</div>";?>
            
            <h4><?=$row_note->title?></h4>
			<br>
            
            <?php 
			$sqlc = "SELECT
						id,
						task_id,
						description,
						by_user,
						files,
						description,
						creation_time
					FROM
						tasks_history
					WHERE
						task_id = '$task_id'
						
						ORDER BY creation_time ASC
					";
			
			
						
			$resultc = query($sqlc);
			
			
			while ($row = mysql_fetch_object($resultc)) {
				
				?>
                <div class="comment <?=($_SESSION['USER_SID'] == $row->by_user)?"me":""?>">
					
					<div class="text">
						<?=stripslashes($row->description)?>
                    </div>
				
                    <div class="files">
                    
                    <?php
                        if($row->files != "") echo  __FILES.': '.get_task_files($row->task_id.'/'.$row->files);
                    ?>
                    </div>
                    
                    <div class="user-date">
                    <?=__BY?> <?=get_user_name($row->by_user).' '.__ON.' '.date('d M Y', strtotime($row->creation_time))?>
                    </div>
                
                </div>
				
			<?php 
            }?>
           
           	<br><br>
           
           	<form action="<?=HTML_BASE.$_SESSION["lang"]?>/correspondence/<?=$task_id?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="task_id" value="<?=$task_id?>" />
			<input type="hidden" name="redirect" value="<?=$redirect?>" />
            
            <h5><?=__ADD_COMMENT?></h5><br>
            <textarea id="body1"  class="inputbox" style="width:100%" name="description" rows="5"><?=sth($_POST["description"])?></textarea>
            
            <?=__ADD_FILES?>:<br>
            <input class="inputbox" name="file1" type="file" />
            <input class="inputbox" name="file2" type="file" />
            <input class="inputbox" name="file3" type="file" />
            
            <br><br>
            <label>
			<input type="checkbox" name="priority" value="1" <?php if($row_note->priority == 1) echo 'checked'; ?>> <?=__MARK_AS_URGENT?></label>
			<br><br>
            
            <input class="button" type="submit" name="submit" id="submit" value="<?=__SEND?>">
			<a href="<?=$redirect?>"><?=__CANCEL?></a>
            
			</form>  
           
			
			
   </div>
    

<?php
foot();
?>